package com.jinly.ryder.manage.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.common.auth.annotation.Auth;
import com.jinly.ryder.common.auth.annotation.AuthParam;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import com.jinly.ryder.manage.service.DatabaseInstanceService;
import com.jinly.ryder.manage.web.mapping.DatabaseInstanceVOMapping;
import com.jinly.ryder.manage.web.vo.DatabaseInstanceVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("manage/database")
@RequiredArgsConstructor
public class DatabaseInstanceController {

    private final DatabaseInstanceVOMapping databaseInstanceVOMapping;
    private final DatabaseInstanceService databaseInstanceService;

    @GetMapping("list")
    @Auth(type = AuthType.APP, role = AuthRole.TESTER, value = "#{appName}")
    public List<DatabaseInstanceVO> groupList(@AuthParam("appName") String appName) {
        return databaseInstanceVOMapping.toVoList(databaseInstanceService.list(appName));
    }

    @GetMapping("page")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public Page<DatabaseInstanceVO> instancePage(Long current, Long size, DatabaseInstanceVO query) {
        return databaseInstanceVOMapping.toVOPage(databaseInstanceService.instancePage(new Page(current, size), databaseInstanceVOMapping.toModel(query)));
    }

    @PostMapping()
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public void save(@RequestBody DatabaseInstanceVO databaseInstanceVO) {
        databaseInstanceService.save(databaseInstanceVOMapping.toModel(databaseInstanceVO));
    }
}
